package com.eigaland.change_machine.business.setup_account

import android.annotation.SuppressLint
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.eigaland.change_machine.R
import com.eigaland.change_machine.business.admin.AdminFragment
import com.eigaland.change_machine.business.movielist.MovieListFragment
import com.eigaland.change_machine.displaymodel.HomeDisplayModel
import com.eigaland.change_machine.displaymodel.IDisplayModel
import com.eigaland.change_machine.util.ProgressDialogManager
import com.eigland.eigaland_lib.base.BaseLazyFragment
import kotlinx.android.synthetic.main.dialog_take_ticket.view.*
import kotlinx.android.synthetic.main.fragment_login.*
import kotlinx.android.synthetic.main.fragment_setup_account.*
import movie6.posprintercept2.extensions.textWatcher

public class SetUpAccountFragment : BaseLazyFragment<SetUpAccountFragmentVM,com.eigaland.change_machine.databinding.FragmentSetupAccountBinding>(){

    var time = 120

    override fun initVM() {
        ed_input_device_pwd.textWatcher {
            this.onTextChanged { charSequence, i1, i2, i3 ->
                vm?.clickableStatus?.postValue((charSequence?.isNotEmpty()!! && ed_input_account.text?.isNotEmpty()!!))
            }
        }


        vm?.progressLoadingStatus?.observe(this, Observer {
            if(it) ProgressDialogManager.showDialog(requireContext()) else ProgressDialogManager.dismissDialog(requireContext())
        })
        vm?.loginStatus?.observe(this, Observer {
            if(it.loginStatus == IDisplayModel.SUCCESS_STATUS){

                if(!arguments?.getBoolean("isAdmin",false)!!) {
                    supportDelegate.replaceFragment(MovieListFragment(), false)
                } else {
                    supportDelegate.startWithPop(AdminFragment())
                }
            } else {
                Toast.makeText(requireContext(),it.errorMsg,Toast.LENGTH_LONG).show()
            }
        })
    }

    override fun setUpViewData(view: View) {
        binding = DataBindingUtil.bind(view)
        vm = SetUpAccountFragmentVM()
        binding?.let {
            it.setupAccountVM = vm
            it.lifecycleOwner = this
        }

        view?.tv_chronometer.start()
        view?.tv_chronometer.text = "120s"
        view?.visibility = View.VISIBLE
        view?.tv_chronometer?.setOnChronometerTickListener {
            it.visibility = View.VISIBLE
            time -= 1
            it.text = time.toString() + "s"
            if(time == 0) supportDelegate?.onBackPressedSupport()

        }

    }

    override fun setLayoutId() = R.layout.fragment_setup_account

}
